package cn.gene.timeyan.ums.webapi.ums.service.impl;

import cn.gene.timeyan.common.exception.TimeyanServiceException;
import cn.gene.timeyan.pojo.admin.model.Audit;
import cn.gene.timeyan.pojo.ums.dto.ReportDTO;
import cn.gene.timeyan.pojo.ums.model.Report;
import cn.gene.timeyan.pojo.ums.vo.ReportVO;
import cn.gene.timeyan.ums.service.IReportService;
import cn.gene.timeyan.ums.webapi.ums.mapper.ReportMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static cn.gene.timeyan.common.restful.ResponseCode.INTERNAL_SERVER_ERROR;

/**
 * <p>
 * 对违规用户的举报 服务实现类
 * </p>
 *
 * @author LWG
 * @since 2022-08-07
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements IReportService {

    @Autowired(required = false)
    private ReportMapper reportMapper;



    @Override
    public void addReportUsers(ReportDTO reportDTO) {
        log.trace("获取reportDTO的相关信息");
        log.trace("reportDTO----->>>>>"+reportDTO);

        //创建举报对象
        Report report = new Report();

        //创建审核对象
        Audit audit = new Audit();

        //查看是否为重复举报
        List<ReportVO> reportVOS = reportMapper.selectReportDTOByHandleState(reportDTO);

        //如果查询结果为不空，说明全部都已处理（或在处理中）
        if (reportVOS.size()>0) {
            log.warn("重复举报相同内容且还未处理的");
            throw new TimeyanServiceException(INTERNAL_SERVER_ERROR,"您已举报过，请等待结果......");
        }


        //说明相同内容没举报过，开始向举报表添加信息
        log.trace("开始向举报表添加信息");
        //补全对象属性
        BeanUtils.copyProperties(reportDTO,report);
        report.setHandleState("0");
        Date now = new Date();
        report.setHandleTime(now);
        report.setUpdateTime(now);
        report.setReportTime(now);
        report.setCreateTime(now);

        //插入数据
        int rows = reportMapper.insertReport(report);

        //判断结果是否为1
        if (rows != 1) {
            throw new TimeyanServiceException(INTERNAL_SERVER_ERROR,"添加举报信息失败，服务器忙，请稍后再次尝试！");
        }
    }
}
